package com.android.qjpmc.widget.retrofithelper.interceptor;

import android.os.Build;

import com.android.qjpmc.manager.AccountManager;
import com.android.qjpmc.widget.retrofithelper.utils.NetUtils;
import com.android.qjpmc.widget.utils.AppUtils;
import com.android.qjpmc.widget.utils.UIUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.MultipartBody;
import okhttp3.Request;
import okhttp3.Response;

public class SecurityInterceptor implements Interceptor {


    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        String sign = "";
        Request.Builder builder = request.newBuilder();
        if (AccountManager.getInstance().isLogin())
            builder.addHeader("token", AccountManager.getInstance().getLoginData(UIUtils.getContext()).accessToken);
        builder.addHeader("deviceType", "2");
        builder.addHeader("osType", "3");
        builder.addHeader("osVersionCode", String.valueOf(Build.VERSION.SDK_INT));
        builder.addHeader("networkType", NetUtils.getNetworkState());
        builder.addHeader("appVersionCode", String.valueOf(AppUtils.getAppInfo().getVersionCode()));
        builder.addHeader("appVersionName", AppUtils.getAppInfo().getVersionName());

       /* builder.addHeader("Pfx-AppVersionName", AppUtils.getAppInfo().getVersionName() + "");
        builder.addHeader("Pfx-DNA-APIVersion", "1.0");
        builder.addHeader("Pfx-OS", "Android");
        builder.addHeader("Pfx-Platform", "Android-App");
        builder.addHeader("Pfx-OS-Version", Build.VERSION.RELEASE);
        builder.addHeader("Pfx-Android-API", Build.VERSION.SDK_INT + "");

        builder.addHeader("Pfx-Device-Name", Build.MODEL);
        builder.addHeader("Pfx-Device-Id", BaseApplication.getAppTagByUUID());
        builder.addHeader("Pfx-Device-Manufacturer", Build.MANUFACTURER);
        builder.addHeader("Pfx-Network", NetUtils.getNetworkState());
        builder.addHeader("Pfx-Screen-Size", DisplayUtils.getScreenInch() + "");
        builder.addHeader("Pfx-Display-Metrics", DisplayUtils.loggerDisplayMetrics() + "");
        builder.addHeader("Pfx-Resolution", DisplayUtils.getScreenWidth(UIUtils.getContext()) + "*" + DisplayUtils.getScreenHeight(UIUtils.getContext()));*/
        Request buildRequest = builder.build();
        return chain.proceed(buildRequest);
    }

    private void injectParamsIntoBody(HashMap<String, String> paramsMap, Request request, Request.Builder requestBuilder) {

        if (request.body() instanceof FormBody) {
            FormBody.Builder newFormBodyBuilder = new FormBody.Builder();
            if (paramsMap.size() > 0) {
                for (Object o : paramsMap.entrySet()) {
                    Map.Entry entry = (Map.Entry) o;
                    newFormBodyBuilder.add((String) entry.getKey(), (String) entry.getValue());

                }
            }
            requestBuilder.post(newFormBodyBuilder.build());

        } else if (request.body() instanceof MultipartBody) {
            MultipartBody.Builder multipartBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM);

            for (Object o : paramsMap.entrySet()) {
                Map.Entry entry = (Map.Entry) o;
                multipartBuilder.addFormDataPart((String) entry.getKey(), (String) entry.getValue());
            }

            List<MultipartBody.Part> oldParts = ((MultipartBody) request.body()).parts();
            if (oldParts != null && oldParts.size() > 0) {
                for (MultipartBody.Part part : oldParts) {
                    multipartBuilder.addPart(part);
                }
            }

            requestBuilder.post(multipartBuilder.build());

        }


    }


}
